<?php

namespace platform\models;

use common\services\Authorization;
use platform\core\BaseModel;
use cm;
use Yii;
use yii\helpers\Url;

class UserModel extends BaseModel
{
    /*
    * 会员管理
    */
    public function actionIndex()
    {
        if (Cm::$req->isPost()) {
            $action = Cm::$req->get('action');
            switch ($action) {
                case "list":
                    $key = Cm::$req->post('key', ['null', 'empty', 'default' => '']);
                    $pageIndex = Cm::$req->post('page', ['null', 'empty', 'default' => 1]);
                    $pageCount = Cm::$req->post('limit', ['null', 'empty', 'default' => 20]);
                    $feedback = Cm::$req->post('feedback', ['null', 'empty', 'default' => '']);
                    $where = " 1=1 AND status=1 ";
                    if(!empty($key)){
                        $where .= " AND (name LIKE '%{$key}%' OR nickname LIKE '%{$key}%' OR telephone LIKE '%{$key}%'  OR id_card LIKE '%{$key}%')";
                    }
                    if($feedback == 1){
                        $where .= " AND (SELECT count(id) AS num FROM feedback WHERE uid=user.id AND status=1)>0 ";
                    }else if($feedback == 2){
                        $where .= " AND (SELECT count(id) AS num FROM feedback WHERE uid=user.id AND status=1)=0 ";
                    }   
                    $data = Cm::$db->paging("SELECT *,(SELECT count(id) AS num FROM feedback WHERE uid=user.id AND status=1) AS feedback FROM user WHERE {$where}",$pageIndex, $pageCount);
                    $snum = ($pageIndex-1)*$pageCount;
                    foreach ($data['data'] as $key => &$value) {
                        $snum++;
                        $value['idnum'] = $snum;
                        $value['head_img'] = empty($value['head_img']) ? '' : $value['head_img'];
                        $value['id_img_p'] = empty($value['id_img_p']) ? '' : $value['id_img_p'];
                        $value['id_img_b'] = empty($value['id_img_b']) ? '' : $value['id_img_b'];
                        $value['feedback'] .= "条";
                    }
                    Cm::$res->paging($data);
                    break;
                case 'del':
                    $id = Cm::$req->post('id', ['null', 'empty', 'default' => 0]);
                    if(empty($id)){
                        Cm::$res->error('请选择删除的会员');
                    }
                    $mdata = ['status' => 2,'updatetime' => date("Y-m-d H:i:s",time())];
                    Cm::$db->update("user", $mdata, "id={$id}");
                    Cm::$app->log->authlog(17,$mdata,$id);
                    Cm::$res->susess();
                    break; 
                case 'contacts':
                    $id = Cm::$req->post('id', ['null', 'empty', 'default' => 0]);
                    if(empty($id)){
                        Cm::$res->error('请选择会员');
                    }
                    $list = Cm::$db->all("SELECT id,name,telephone,id_card FROM user_contacts WHERE uid={$id} AND status=1");
                    Cm::$app->log->authlog(18,$list,$id);
                    Cm::$res->susess($list);    
                    break;  
                case 'feedback':
                    $id = Cm::$req->post('id', ['null', 'empty', 'default' => 0]);
                    if(empty($id)){
                        Cm::$res->error('请选择会员');
                    }  
                    $list = Cm::$db->all("SELECT id,content,addtime FROM feedback WHERE uid={$id} AND status=1");
                    Cm::$app->log->authlog(18,$list,$id);
                    foreach ($list as $key => &$value) {
                        $value['ids'] = $key+1;
                    }
                    Cm::$db->update("feedback",['issee'=>1,'updatetime'=>date("Y-m-d H:i:s",time())],"uid={$id} AND issee=0");
                    Cm::$res->susess($list);    
                    break;      
            }
        }
        
        $data['map'] = [];
        $data['map'][] = ['title' => "会员管理", 'url' => Url::to(['user/index'])];
        $data['baseUrl'] = Url::to(['user/index']);
        return $data;
    }
}